<template>
  <base-dialog
    :title="title + '参数'"
    ref="baseDialog"
    @handleClose="handleClose"
    @handleSubmit="handleSubmit"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="参数名称" prop="configName">
        <el-input
          v-model="form.configName"
          placeholder="请输入参数名称"
          show-word-limit
          clearable
          :maxlength="20"
        />
      </el-form-item>
      <el-form-item label="参数键名" prop="configKey">
        <el-input
          v-model="form.configKey"
          placeholder="请输入参数键名"
          show-word-limit
          clearable
          :maxlength="50"
        />
      </el-form-item>
      <el-form-item label="参数键值" prop="configValue">
        <el-input
          ref="areaInfo"
          type="textarea"
          v-model="form.configValue"
          placeholder="请输入参数键值"
          show-word-limit
          clearable
          :autosize="{ minRows: 2, maxRows: 12 }"
          :maxlength="600"
        />
      </el-form-item>
      <el-form-item label="系统内置" prop="configSys">
        <el-radio-group v-model="form.configSys">
          <el-radio label="Y">是</el-radio>
          <el-radio label="N">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          placeholder="请输入备注"
          show-word-limit
          clearable
          :autosize="{ minRows: 2, maxRows: 12 }"
          :maxlength="200"
        />
      </el-form-item>
    </el-form>
  </base-dialog>
</template>

<script>
import { add, edit } from "@/api/system/config"
export default {
  name: "AddEditDialog",
  data() {
    return {
      title: "",
      form: {
        configSys: "Y"
      },
      rules: {
        configName: [{ required: true, message: "参数名称不能为空", trigger: "blur" }],
        configKey: [{ required: true, message: "参数键名不能为空", trigger: "blur" }],
        configValue: [{ required: true, message: "参数键值不能为空", trigger: "blur" }]
      }
    }
  },
  methods: {
    init(item) {
      this.$refs.baseDialog.dialogVisible = true
      if (item) {
        this.title = "编辑"
        this.form = JSON.parse(JSON.stringify(item))
      } else {
        this.title = "新增"
      }
      this.$refs.areaInfo.resizeTextarea()
    },
    handleClose() {
      this.form = {
        configSys: "Y"
      }
      this.$refs.baseDialog.dialogVisible = false
      this.title = ""
    },
    // 提交表单
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.title === "编辑") {
            await edit(this.form)
            this.$message({
              type: "success",
              message: "参数编辑成功"
            })
            this.$emit("updatePage")
            this.handleClose()
          } else {
            await add(this.form)
            this.$message({
              type: "success",
              message: "参数新增成功"
            })
            this.$emit("updatePage")
            this.handleClose()
          }
        }
      })
    }
  }
}
</script>
